﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TripsLogic.controller;
using TripsLogic.WeSStatus;

namespace TripsLogic
{
    public partial class activate_account : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                loadLang();
                checkActivateAccount();
            }
        }

        private void checkActivateAccount()
        {
            if (Request.QueryString["idtoken"] != null && Request.QueryString["idtoken"].ToString() != "")
            {
                string[] idtoken = Request.QueryString["idtoken"].ToString().Split('-');
                string id = idtoken[0];
                string token = idtoken[1];

                int _id = -1;
                try
                {
                    _id = int.Parse(id);
                }
                catch (Exception ex)
                {

                }
                WeSSysUser.ActivateAccountResult result = new WeSSysUser.WeSSysUserClient().activateAccount(_id, token);

                DataTable dt = SysUserController.getUserById(id);
                if (result.sysUserResultStatus.statusCode == ActivateAccountResult.STATUS_VALID_TOKEN_CODE)
                {
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        Session["Email"] = dt.Rows[0]["Email"].ToString();
                        Session["UserId"] = dt.Rows[0]["Id"].ToString();
                        string url = "";
                        if (Session["RefUrl"] != null && Session["RefUrl"].ToString() != "")
                        {
                            if (Session["RefUrl"].ToString().Contains("info-"))
                                url = Session["RefUrl"].ToString() + "#_comment";
                            else
                                url = Session["RefUrl"].ToString();
                        }
                        else
                            url = "Default";
                        string activateResponse = String.Format(LangMsgController.getLngMsg("litActivateResponse.OK", "Account <font style='font-weight:bold'><u>{0}</font></u> has been activted successfully. " +
                            "Click <a class='link3' href='{1}'>here</a> to start."), dt.Rows[0]["Email"].ToString(), url);
                        this.litActivateResponse.Text = activateResponse;
                    }
                }
                else
                {
                    if (result.sysUserResultStatus.statusCode != ActivateAccountResult.STATUS_ALREADY_ACTIVATED_CODE)
                        this.litActivateResponse.Text = String.Format(LangMsgController.getLngMsg("litActivateResponse.Error", "Error occurs in activating account <font style='font-weight:bold'><u>{0}</font></u>. <br/>" +
                            "<font style='font-weight:bold'>Error</font>: {1} <br/>You can try again or <a class='link3' href='reset-activated-code'>reset activated code</a> from the system."), dt.Rows[0]["Email"].ToString(), result.sysUserResultStatus.statusText);
                    else
                        this.litActivateResponse.Text = String.Format(LangMsgController.getLngMsg("litAlreadyActivateResponse.Error", "Error occurs in the activating account <font style='font-weight:bold'><u>{0}</font></u>. <br/>" +
                            "<font style='font-weight:bold'>Error</font>: {1}"), dt.Rows[0]["Email"].ToString(), result.sysUserResultStatus.statusText);
                }
            }
        }

        private void loadLang()
        {
            this.litActivateAccount.Text = LangController.getLng("litActivateAccount.Text", "Activate your account");
            this.Title = LangController.getLng("litWebsiteTitle.Text", "TripsLogic") + " - " + this.litActivateAccount.Text;
        }
    }
}